/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.snda.dw.lidejia.hadooptotalsort.type;

import com.snda.dw.lidejia.hadooptotalsort.meta.SortKeyMetaPool;
import com.snda.dw.lidejia.hadooptotalsort.meta.SortKeyMeta;
import java.util.List;
import org.apache.hadoop.io.WritableComparator;

/**
 *
 * @author lidejia
 */
public class SortKeysComparator extends WritableComparator {

    private List<SortKeyMeta> metas;

    public SortKeysComparator() {
        super(SortKeys.class);
        this.metas = SortKeyMetaPool.getInstance().getMetas();
    }

    @Override
    public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
        int cmp = 0;
        for (SortKeyMeta meta : metas) {
            int fieldLength1 = meta.getLength(b1, s1);
            int fieldLength2 = meta.getLength(b2, s2);
            cmp = meta.compare(b1, s1, fieldLength1, b2, s2, fieldLength2);
            if (cmp != 0) {
                break;
            } else {
                s1 += fieldLength1;
                s2 += fieldLength2;
            }
        }
        return cmp;
    }
}
